<?php

/**
 * category actions.
 *
 * @package    lomejorde
 * @subpackage category
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class categoryActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->categories = $this->getRoute()->getObjects();
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->category = $this->getRoute()->getObject();
    }
    
    public function executeShowCandidates(sfWebRequest $request)
    {
        $this->category = $this->getRoute()->getObject();
        
        $this->pager = new sfDoctrinePager('Candidate', 30);
        
        $this->pager->setQuery($this->setCandidatesPagerQuery($this->category->getId()));
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();
    }
    
    public function executeShowNominees(sfWebRequest $request)
    {
        $this->category = $this->getRoute()->getObject();
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = new CategoryForm();
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->form = new CategoryForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        $this->form = new CategoryForm($this->getRoute()->getObject());
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->form = new CategoryForm($this->getRoute()->getObject());

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        $request->checkCSRFProtection();

        $this->getRoute()->getObject()->delete();

        $this->redirect('@category');
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $category = $form->save();

            $this->redirect('@category_show?id='.$category->getId());
        }
    }
    
    public function executeSearchCandidates(sfWebRequest $request)
    {
        $this->pager = new sfDoctrinePager('Candidate', 30);
        
        $this->pager->setQuery($this->setPagerQuery($request));
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();

        if ($request->isXmlHttpRequest())
        {
            if ('*' == $query || !$this->pager->getResults())
            {
                return $this->renderText('No hay resultados que coincidan con la busqueda.');
            }
            
            return $this->renderPartial('list', array('pager' => $this->pager, 'query' => $request->getParameter('query','')));
        } 
        else 
        {
            $this->setTemplate('index');
        }
    }
    
    private function setCandidatesPagerQuery($category_id = '', $str = '') {
        $query = Doctrine_Query::create()->from('Candidate')->orderBy('name');
        
        if($str !== ''){
            $query = Doctrine_Core::getTable('Candidate')->addCandidatesByNameOrDescriptionQuery($str, $query);
        }
        
        if($category_id !== ''){
            $query = Doctrine_Core::getTable('Candidate')->addCandidatesByCategoriesQuery($category_id, $query);
        }
        
        return $query;
    }
    
    public function executeAddNominee(sfWebRequest $request){
        $this->category = $this->getRoute()->getObject();
        
        $nominee = new Nominee();
        
        $nominee->setCategory($this->category);
        
        $this->form = new NomineeForm($nominee);
    }
    
    public function executeGetCandidates(sfWebRequest $request)
    {
        $category = $this->getRoute()->getObject();
        
        $this->getResponse()->setContentType('application/json');

        $string = $request->getParameter('term');

        $limit = $request->getParameter('limit',8);

        $results = Doctrine::getTable('Candidate')->getCandidatesByCategoryIdAndNameLike($category->getId(), $string, $limit);
        
        $candidates = array();

        foreach ($results as $key => $candidate) {
            $c = array();
            
            $c['label'] = $candidate->getName();
            
            $c['desc'] = $candidate->getTwitter() ?  '@'.$candidate->getTwitter() : $candidate->getDescription() ? $candidate->getDescription() : '';
            
            $c['icon'] = $candidate->getThumb();
            
            $c['value'] = $candidate->getId();
            
            $candidates[$key] = $c;
        }

        return $this->renderText(json_encode($candidates));
    }
}
